/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/

package virtPresenter.model
{
	import flash.external.*;
	import flash.utils.*;
	
	/**
	 * Class reads the querystring from the url. This is needed
	 * for directly accessing a lecture via the url.
	 */
	public class QueryString 
	{
		private var _queryString:String;
		private var _all:String;
		private var _params:Object;
		
		/**
		 * Getter method for the query string.
		 * 
		 * @return String Query string 
		 */
		public function get queryString():String
		{
			return _queryString;
		}
		
		/**
		 * Getter method for the whole url.
		 * 
		 * @return String Url 
		 */
		public function get url():String
		{
			return _all;
		}
		
		/**
		 * Getter method for the paramter list.
		 * 
		 * @return Object Paramter list
		 */
		public function get parameters():Object
		{
			return _params;
		}	
		
		/**
		 * Constructor of QueryString.
		 */
		public function QueryString()
		{		
			readQueryString();
		}

		private function readQueryString():void
		{
			_params = {};
			try 
			{
				_all =  ExternalInterface.call("window.location.href.toString");
				_queryString = ExternalInterface.call("window.location.search.substring", 1);
				if(_queryString)
				{				
					var params:Array = _queryString.split('&');
					var length:uint = params.length;
					
					for (var i:uint=0,index:int=-1; i<length; i++) 
					{
						var kvPair:String = params[i];
						if((index = kvPair.indexOf("=")) > 0)
						{
							var key:String = kvPair.substring(0,index);
							var value:String = kvPair.substring(index+1);
							_params[key] = value;
						}
					}
				}
			}catch(e:Error) { trace("QueryString. Some error occured: ExternalInterface doesn't work in Standalone player."); }
		}

	}
}
